//
//  HomeRemmendCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/8/19.
//

import UIKit

class HomeRemmendCell: UITableViewCell {
    var chooseBag:((Int) -> Void)?
    private lazy var likeBtn: XFButton = {
        let button = XFButton(type: .custom)
        button.normalBackgroundImage = UIImage(named: "icon_home_onebg")
        button.imageView?.contentMode = .scaleAspectFit
        return button
    }()
    
    private lazy var lastBtn: XFButton = {
        let button = XFButton(type: .custom)
        button.normalBackgroundImage = UIImage(named: "icon_home_twobg")
        button.imageView?.contentMode = .scaleAspectFit
        return button
    }()
    
    private lazy var offlineBtn: XFButton = {
        let button = XFButton(type: .custom)
        button.normalBackgroundImage = UIImage(named: "icon_home_threeBg")
        button.imageView?.contentMode = .scaleAspectFit
        return button
    }()
    
    private lazy var mostlyBtn: XFButton = {
        let button = XFButton(type: .custom)
        button.normalBackgroundImage = UIImage(named: "icon_home_fourbg")
        button.imageView?.contentMode = .scaleAspectFit
        return button
    }()
    
    private lazy var likeLb: UILabel = {
       let label = UILabel()
        label.textColor = .white
        label.numberOfLines = 2
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        return label
    }()
    
    private lazy var lastLb: UILabel = {
       let label = UILabel()
        label.textColor = .white
        label.numberOfLines = 2
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        return label
    }()
    
    private lazy var offlineLb: UILabel = {
       let label = UILabel()
        label.textColor = .white
        label.numberOfLines = 2
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        return label
    }()
    
    private lazy var mostlyLb: UILabel = {
       let label = UILabel()
        label.textColor = .white
        label.numberOfLines = 2
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        return label
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        
        contentView.addSubviews([likeBtn, lastBtn, offlineBtn, mostlyBtn, likeLb, lastLb, offlineLb, mostlyLb])
        
        likeBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalToSuperview().offset(14)
            make.trailing.equalTo(lastBtn.snp.leading).offset(0)
            make.width.equalTo(((SCREEN_WIDTH) / 2) + 20)
            make.height.equalTo(GET_WIDTH(W: 62))
        }
        
        lastBtn.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.leading.equalTo(likeBtn.snp.trailing).offset(0)
            make.trailing.equalToSuperview().offset(-14)
            make.height.equalTo(GET_WIDTH(W: 62))
        }
        
        offlineBtn.snp.makeConstraints { make in
            make.top.equalTo(likeBtn.snp.bottom).offset(10)
            make.leading.equalToSuperview().offset(14)
            make.width.equalTo((SCREEN_WIDTH - 48) / 2)
            make.height.equalTo(GET_WIDTH(W: 62))        }
        
        mostlyBtn.snp.makeConstraints { make in
            make.top.equalTo(lastBtn.snp.bottom).offset(10)
            make.leading.equalTo(offlineBtn.snp.trailing)
            make.trailing.equalToSuperview().offset(-14)
            make.height.equalTo(GET_WIDTH(W: 62))
        }
        
        likeLb.snp.makeConstraints { make in
            make.top.equalTo(likeBtn.snp.top).offset(10)
            make.leading.equalTo(likeBtn.snp.leading).offset(15)
            make.trailing.equalTo(likeBtn.snp.trailing).offset(-10)
        }
        
        lastLb.snp.makeConstraints { make in
            make.top.equalTo(lastBtn.snp.top).offset(10)
            make.leading.equalTo(lastBtn.snp.leading).offset(20)
            make.trailing.equalTo(lastBtn.snp.trailing).offset(-10)
        }
        
        offlineLb.snp.makeConstraints { make in
            make.top.equalTo(offlineBtn.snp.top).offset(10)
            make.leading.equalTo(offlineBtn.snp.leading).offset(15)
            make.trailing.equalTo(offlineBtn.snp.trailing).offset(-10)
        }
        
        mostlyLb.snp.makeConstraints { make in
            make.top.equalTo(mostlyBtn.snp.top).offset(10)
            make.leading.equalTo(mostlyBtn.snp.leading).offset(20)
            make.trailing.equalTo(mostlyBtn.snp.trailing).offset(-10)
        }
        
        likeBtn.setClosureTouchUpInside_k { [weak self] in
            self?.chooseBag?(0)
        }
        
        lastBtn.setClosureTouchUpInside_k { [weak self] in
            self?.chooseBag?(1)
        }
        
        offlineBtn.setClosureTouchUpInside_k { [weak self] in
            self?.chooseBag?(2)
        }
        
        mostlyBtn.setClosureTouchUpInside_k { [weak self] in
            self?.chooseBag?(3)
        }
        
    }
    
    func setData(models:[PlazaListModel]){
        if models.count >= 1 {
            likeLb.text = models[0].item?.first?.question ?? ""
        }
        if models.count >= 2 {
            lastLb.text = models[1].item?.first?.question ?? ""
        }
        if models.count >= 3 {
            offlineLb.text = models[2].item?.first?.question ?? ""
        }
        if models.count >= 4 {
            mostlyLb.text = models[3].item?.first?.question ?? ""
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
